package com.itgungnir.diyshuo.fragment.fenlei_items_fragment;

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.itgungnir.diyshuo.activity.R;
import com.itgungnir.diyshuo.activity.listview_activity.ListViewActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分类页面两个Fragment中相当于MVP模式中Model层的类
 */
public class FenleiItemModel implements FenleiItemContract.Model {
    @Override
    public void setTextToTextView(TextView tv, String text) {
        tv.setText(text);
    }

    @Override
    public void setContentToGridView(FragmentActivity activity, GridView gv, int[] images, String[] titles) {
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < images.length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("image", images[i]);
            map.put("title", titles[i]);
            list.add(map);
        }
        gv.setAdapter(new SimpleAdapter(activity, list, R.layout.griditem_fenleimainvpitem,
                new String[]{"image", "title"},
                new int[]{R.id.find_fenleimainvpitem_iv_image, R.id.find_fenleimainvpitem_tv_title}));
    }

    @Override
    public void onGridViewItemClickListener(FragmentActivity activity, int position, String title, boolean isAdd) {
        Intent intent = new Intent(activity, ListViewActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("isAdd", isAdd);
        intent.putExtra("id", (position + 1) + "");
        activity.startActivity(intent);
    }
}